package com.example.kotlintest.chat.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.kotlintest.R;

import java.util.Arrays;
import java.util.List;
/**
 * @author linzi
 * @date 2022-9-28 13:50
 * @context 菜单适配器
 */
public class BottomMenuAdapter extends RecyclerView.Adapter<BottomMenuAdapter.MenuHolder> {

    public static void init(Context context,RecyclerView recyclerView,OnItemClickListener listener){
        ViewGroup.LayoutParams params=recyclerView.getLayoutParams();
        params.height=ViewGroup.LayoutParams.WRAP_CONTENT;
        BottomMenuAdapter adapter=new BottomMenuAdapter(context,listener);
        GridLayoutManager gridLayoutManager=new GridLayoutManager(context,4){
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setLayoutParams(params);
        recyclerView.setAdapter(adapter);
    }

    private Context mContext;
    private List<MenuInfo> menus= Arrays.asList(new MenuInfo(R.drawable.icon_photos,"相册"),
            new MenuInfo(R.drawable.icon_take_photo,"拍照"),
            new MenuInfo(R.drawable.icon_video,"视频通话"),
            new MenuInfo(R.drawable.icon_location,"位置"),
            new MenuInfo(R.drawable.icon_files,"文件")
    );
    private OnItemClickListener onItemClickListener;

    private BottomMenuAdapter(Context mContext,OnItemClickListener onItemClickListener) {
        this.mContext = mContext;
        this.onItemClickListener=onItemClickListener;
    }

    @NonNull
    @Override
    public MenuHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.chat_item_bottom_menu,parent,false);
        return new MenuHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MenuHolder holder, @SuppressLint("RecyclerView") int position) {
        MenuInfo info=menus.get(position);
        holder.ivIcon.setBackground(mContext.getDrawable(info.getRes()));
        holder.tvMenuName.setText(info.getMenu());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClickListener.itemClick(holder,position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return menus.size();
    }

    public static class MenuHolder extends RecyclerView.ViewHolder{
        public ImageView ivIcon;
        public TextView tvMenuName;
        public MenuHolder(@NonNull View itemView) {
            super(itemView);
            ivIcon=itemView.findViewById(R.id.ivIcon);
            tvMenuName=itemView.findViewById(R.id.tvMenuName);
        }
    }

    class MenuInfo {
        private int res;
        private String menu;

        public MenuInfo(int res, String menu) {
            this.res = res;
            this.menu = menu;
        }

        public int getRes() {
            return res;
        }

        public String getMenu() {
            return menu;
        }
    }

    public interface OnItemClickListener{
        void itemClick(MenuHolder menuHolder,int postion);
    }
}
